import { API_BASE_URL, LAYOUT_PATH } from '@/config/setting'
import router from '@/router'
/**
 * axios实例
 */
import axios from 'axios'
import { ElMessageBox } from 'element-plus/es'
import { unref } from 'vue'
import { logout, toURLSearch } from './common'
import { getMchId, getToken, setToken } from './token-util'

/** 创建axios实例 */
const service = axios.create({
  baseURL: API_BASE_URL,
})

/**
 * 新增请求拦截器
 */
service.interceptors.request.use(
  (config) => {
    requestInterceptor(config)
    return config
  },
  (error) => {
    console.error(error)
    return Promise.reject(new Error('网络错误'))
  },
)

/**
 * 新增响应拦截器
 */
service.interceptors.response.use(
  (res) => {
    const errorMessage = responseInterceptor(res)
    if (errorMessage) {
      return Promise.reject(new Error(errorMessage))
    }
    res.data.success = res.data.code === 0
    const keys = res.data.data && typeof res.data.data === 'object' ? Object.keys(res.data.data) : []
    if (keys.length && ['list', 'count'].every(k => keys.includes(k))) {
      keys.forEach((k) => {
        if (!['list', 'count'].includes(k))
          res.data[k] = res.data.data[k]
      })
      res.data.count = res.data.data.count
      res.data.data = res.data.data.list
    }
    return res.data
  },
  (error) => {
    console.error(error)
    return Promise.reject(new Error('网络错误'))
  },
)

/**
 * 请求拦截处理
 */
export function requestInterceptor(config) {
  // 新增token到header
  if (config.headers) {
    const token = getToken()
    const mchId = getMchId()
    if (token) {
      config.headers.Token = token
    }
    if (mchId) {
      config.headers['Mch-Id'] = mchId
    }
  }

  // get请求处理数组和对象类型参数
  if (config.method === 'get' && config.params) {
    config.url = toURLSearch(config.params, config.url)
    config.params = {}
  }
}

/**
 * 响应拦截处理
 */
export function responseInterceptor(res) {
  // 登录过期处理
  if (res.data?.code > 1) {
    const { path, fullPath } = unref(router.currentRoute)
    if (path === LAYOUT_PATH) {
      logout(true, void 0, router.push)
    }
    else if (path !== '/login') {
      ElMessageBox.close()
      ElMessageBox.alert('登录状态已过期, 请退出重新登录!', '系统提示', {
        confirmButtonText: '重新登录',
        callback: (action) => {
          if (action === 'confirm') {
            logout(false, fullPath)
          }
        },
        type: 'warning',
        draggable: true,
      })
    }
    return res.data.message
  }
  // 续期token
  const newToken = res.headers.authorization
  if (newToken) {
    setToken(newToken)
  }
}

export default service
